package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	未完成
 * http://finance.jrj.com.cn/people/2013/05/06122615278301.shtml  金融界财经频道
 *
 * @author KK
 *
 */
public class JrjNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.jrj.com.cn/.*[0-9]{4}/[0-9]{2}/[0-9]+[\\-0-9]*.shtml",
		"http://.*.jrj.com.cn/share,disc,[0-9]{4}-[0-9]{2}-[0-9]{2},[0-9]+,[a-z0-9]+.shtml"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);
		doc.select("h1 p,div.textimg ").remove();
		title=doc.select("h3.tit").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("h1").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, "-");
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.newsCon"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.textmain.tmf14.jrj-clear"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("p.p1"));
		}

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("p.newsource").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("div.newsource").text();
		}
		author=StringUtils.substringBetween(authorStr, "来源：","   ");

		date=DateUtils.matchDate(doc.select("p.newsource").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("div.newsource").text());
		}
		if(date == null){
			date=DateUtils.matchDate(doc.select("p.tc").text());
		}
		if (StringUtils.isNotBlank(title) && date!=null){
			news.setTitle(title);
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);
			news.setType(1);
		}
		return news;
	}
	public static void main(String[] args) {
		String url="http://finance.jrj.com.cn/2014/08/04054717731863.shtml";
		JrjNewsAnalyse analyse=new JrjNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
